﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PMS.Common;

namespace PMS.App
{
    public partial class ucComponentHierachy : UserControl
    {
        #region Field
          private TreeNode dragNode = null;
          private TreeNode tempDropNode = null;
          private Timer timer = new Timer();
        #endregion

        public ucComponentHierachy()
        {
            InitializeComponent();
            timer.Interval = 200;
            timer.Tick += new EventHandler(timer_Tick);
            this.ContextMenuStrip = cmsComp;
        }

        private void treevHeThongThietBi_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                tvHeThongThietBi.BeginUpdate();
                this.tvHeThongThietBi.SelectedNode = e.Node;
                e.Node.ContextMenuStrip = cmsTvHeThongThietBi;
                e.Node.ContextMenuStrip.Show(MousePosition);
                tvHeThongThietBi.EndUpdate();
            }
        }
      
        private void treevHeThongThietBi_ItemDrag(object sender, ItemDragEventArgs e)
        {
            this.dragNode = (TreeNode)e.Item;
            this.tvHeThongThietBi.SelectedNode = this.dragNode;

            this.imageListDrag.Images.Clear();
            this.imageListDrag.ImageSize = new Size(this.dragNode.Bounds.Size.Width + this.tvHeThongThietBi.Indent, this.dragNode.Bounds.Height);

            Bitmap bmp = new Bitmap(this.dragNode.Bounds.Width + this.tvHeThongThietBi.Indent, this.dragNode.Bounds.Height);

            Graphics gfx = Graphics.FromImage(bmp);

            gfx.DrawImage(this.imageListTreeView.Images[0], 0, 0);

            gfx.DrawString(this.dragNode.Text,
                this.tvHeThongThietBi.Font,
                new SolidBrush(this.tvHeThongThietBi.ForeColor),
                (float)this.tvHeThongThietBi.Indent, 1.0f);

            this.imageListDrag.Images.Add(bmp);

            Point p = this.tvHeThongThietBi.PointToClient(Control.MousePosition);

            int dx = p.X + this.tvHeThongThietBi.Indent - this.dragNode.Bounds.Left;
            int dy = p.Y - this.dragNode.Bounds.Top;

            if (DragHelper.ImageList_BeginDrag(this.imageListDrag.Handle, 0, dx, dy))
            {
                this.tvHeThongThietBi.DoDragDrop(bmp, DragDropEffects.Move);
                DragHelper.ImageList_EndDrag();
            }		
        }

        private void tvHeThongThietBi_DragOver(object sender, DragEventArgs e)
        {

            Point formP = this.PointToClient(new Point(e.X, e.Y));
            DragHelper.ImageList_DragMove(formP.X - this.tvHeThongThietBi.Left, formP.Y - this.tvHeThongThietBi.Top);

            TreeNode dropNode = this.tvHeThongThietBi.GetNodeAt(this.tvHeThongThietBi.PointToClient(new Point(e.X, e.Y)));
            if (dropNode == null)
            {
                e.Effect = DragDropEffects.None;
                return;
            }

            e.Effect = DragDropEffects.Move;

            if (this.tempDropNode != dropNode)
            {
                DragHelper.ImageList_DragShowNolock(false);
                this.tvHeThongThietBi.SelectedNode = dropNode;
                DragHelper.ImageList_DragShowNolock(true);
                tempDropNode = dropNode;
            }

            TreeNode tmpNode = dropNode;
            while (tmpNode.Parent != null)
            {
                if (tmpNode.Parent == this.dragNode) e.Effect = DragDropEffects.None;
                tmpNode = tmpNode.Parent;
            }
        }

        private void tvHeThongThietBi_DragDrop(object sender, DragEventArgs e)
        {

            DragHelper.ImageList_DragLeave(this.tvHeThongThietBi.Handle);

            TreeNode dropNode = this.tvHeThongThietBi.GetNodeAt(this.tvHeThongThietBi.PointToClient(new Point(e.X, e.Y)));

            if (this.dragNode != dropNode)
            {
                if (this.dragNode.Parent == null)
                {
                    this.tvHeThongThietBi.Nodes.Remove(this.dragNode);
                }
                else
                {
                    this.dragNode.Parent.Nodes.Remove(this.dragNode);
                }

                dropNode.Nodes.Add(this.dragNode);
                dropNode.ExpandAll();

                this.dragNode = null;

                this.timer.Enabled = false;
            }
        }

        private void tvHeThongThietBi_DragEnter(object sender, DragEventArgs e)
        {
            DragHelper.ImageList_DragEnter(this.tvHeThongThietBi.Handle, e.X - this.tvHeThongThietBi.Left,
              e.Y - this.tvHeThongThietBi.Top);

            this.timer.Enabled = true;
        }

        private void tvHeThongThietBi_DragLeave(object sender, EventArgs e)
        {
            DragHelper.ImageList_DragLeave(this.tvHeThongThietBi.Handle);

            this.timer.Enabled = false;
        }

        private void tvHeThongThietBi_GiveFeedback(object sender, GiveFeedbackEventArgs e)
        {

            if (e.Effect == DragDropEffects.Move)
            {
                e.UseDefaultCursors = false;
                this.tvHeThongThietBi.Cursor = Cursors.Default;
            }
            else e.UseDefaultCursors = true;
        }
        private void timer_Tick(object sender, EventArgs e)
        {
            Point pt = PointToClient(Control.MousePosition);
            TreeNode node = this.tvHeThongThietBi.GetNodeAt(pt);

            if (node == null) return;

            if (pt.Y < 30)
            {
                if (node.PrevVisibleNode != null)
                {
                    node = node.PrevVisibleNode;

                    DragHelper.ImageList_DragShowNolock(false);
                    node.EnsureVisible();
                    this.tvHeThongThietBi.Refresh();
                    DragHelper.ImageList_DragShowNolock(true);

                }
            }
            else if (pt.Y > this.tvHeThongThietBi.Size.Height - 30)
            {
                if (node.NextVisibleNode != null)
                {
                    node = node.NextVisibleNode;

                    DragHelper.ImageList_DragShowNolock(false);
                    node.EnsureVisible();
                    this.tvHeThongThietBi.Refresh();
                    DragHelper.ImageList_DragShowNolock(true);
                }
            }
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {

            var hitTest = tvHeThongThietBi.HitTest(tvHeThongThietBi.PointToClient(new Point(cmsTvHeThongThietBi.Left, cmsTvHeThongThietBi.Top)));
            if (null != hitTest)
            {
                tvHeThongThietBi.SelectedNode = hitTest.Node;
                TreeNode newNode = new TreeNode();
                newNode.Text = "New node";
                tvHeThongThietBi.SelectedNode.Nodes.Add(newNode);
                tvHeThongThietBi.SelectedNode.Expand();
                this.tvHeThongThietBi.LabelEdit = true;
                newNode.BeginEdit();
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var hitTest = tvHeThongThietBi.HitTest(tvHeThongThietBi.PointToClient(new Point(cmsTvHeThongThietBi.Left, cmsTvHeThongThietBi.Top)));
            if (null != hitTest)
            {
                tvHeThongThietBi.SelectedNode = hitTest.Node;
                tvHeThongThietBi.Nodes.Remove(tvHeThongThietBi.SelectedNode);
            }
        }

        string selectedNodeText;
        private void renameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TreeNode node = this.tvHeThongThietBi.SelectedNode as TreeNode;
            selectedNodeText = node.Text;
            this.tvHeThongThietBi.LabelEdit = true;
            node.BeginEdit();
        }

        private void tvHeThongThietBi_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
        {
            try
            {
                TreeNode node = this.tvHeThongThietBi.SelectedNode as TreeNode;
                this.tvHeThongThietBi.LabelEdit = false;

                if (e.Label.IndexOfAny(new char[] { '\\', '/', ':', '*', '?', '"', '<', '>', '|' }) != -1)
                {
                    MessageBox.Show("Invalid StepName.\n" +
                      "The step Name must not contain " +
                          "following characters:\n \\ / : * ? \" < > |",
                      "step name Edit Error", MessageBoxButtons.OK,
                      MessageBoxIcon.Error);
                    e.CancelEdit = true;
                    return;
                }
                if (string.IsNullOrWhiteSpace(e.Label))
                {
                    MessageBox.Show("Step name is Invalid");
                    e.CancelEdit = true;
                    return;
                }
                string label = (!string.IsNullOrEmpty(e.Label) ? e.Label : selectedNodeText);
                if (null != e.Label)
                {
                    node.Name = label;
                }
            }
            catch(Exception ex)
            {

            }
        }

        private void xtraTabJobs_SizeChanged(object sender, EventArgs e)
        {
          
        }

        private void ucComponentHierachy_MouseClick(object sender, MouseEventArgs e)
        {

            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                this.ContextMenuStrip.Show(MousePosition);
            }
           
        }

        private void xtraTabControl1_MouseClick(object sender, MouseEventArgs e)
        {
          
        }
        public void SetValue(string _str)
        {
            textEdit17.Location = new Point(txtCost.Location.X, txtCost.Location.Y);
            MessageBox.Show(txtCost.Location.X.ToString() +"-"+ txtCost.Location.Y.ToString());

        }
        private void findF3ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            frmFindComponentOnTree frmComp = new frmFindComponentOnTree();
            frmComp.StartPosition = FormStartPosition.CenterScreen;
            frmComp.Show();
        }
    }
}
